﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.Model;
using Yipusoft.SixAcquaint.Model;
using Yipusoft.SixAcquaint.BLL;
using System.Collections.Generic;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Account.BLL;

public partial class BasicBase_WaterSource_SearchWater : System.Web.UI.Page
{
    private static string sqlString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindProvince();
            ddlProvinces.SelectedValue = "SS030000";
            BindCities(ddlProvinces.SelectedValue);

            //部门
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            else
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
        }
    }

    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {
        DepartmentListBind();
    }

    void DepartmentListBind()
    {
        if (DepartmentList.SelectedValue == "")
        {
            UnitList.Items.Clear();
            UnitList.Items.Add(new ListItem("----部门----", ""));
            UnitList.DataSource = null;
            UnitList.DataBind();
        }
        else
        {
            UnitList.Items.Clear();
            UnitList.Items.Add(new ListItem("----部门----", ""));
            string strWhere = "(departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0') or departmentid='" + DepartmentList.SelectedValue + "'";
            UnitList.DataSource = Department.GetList(strWhere);
            UnitList.DataBind();
        }
    }

    private void BindWater(string strSql, int pageSize, int pageIndex)
    {
        if (!panPage.Visible)
            panPage.Visible = true;
        int totalCount = Water.GetWaterCountByWhere(strSql);
        //pageIndex = pageIndex + 1;
        ViewState["PageCount"] = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        if ((int)ViewState["PageCount"] < pageIndex)
            pageIndex = pageIndex - 1;
        ViewState["PageIndex"] = pageIndex;
        gvWater.DataSource = Water.GetWaterList(strSql, "RoadID", "Desc", pageSize, pageIndex);
        gvWater.DataBind();
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    private void BindProvince()
    {

        ListItem blankLI = new ListItem("", "");
        ddlProvinces.Items.Add(blankLI);
        foreach (CodeItemInfo item in CodeItem.GetList("ParentID='SS000000' ORDER BY SeqID"))
        {
            ListItem li = new ListItem(item.ItemName, item.ItemID);
            ddlProvinces.Items.Add(li);
        }

    }

    protected void ddlProvinces_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(ddlProvinces.SelectedValue);
    }

    private void BindCities(string province)
    {

        ddlCities.Items.Clear();
        if (province.Trim() == "")
        {
            ListItem blankLI = new ListItem("", "");
            ddlCities.Items.Add(blankLI);
        }
        else
        {
            foreach (CodeItemInfo item in CodeItem.GetList("ParentID='" + province + "' Order by SeqID"))
            {
                ListItem li = new ListItem(item.ItemName, item.ItemID);
                ddlCities.Items.Add(li);
                foreach (CodeItemInfo subItem in CodeItem.GetList("ParentID='" + item.ItemID + "' Order by SeqID"))
                    ddlCities.Items.Add(new ListItem("\u3000" + subItem.ItemName, subItem.ItemID));
            }
        }
    }

    //private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    //{
    //    if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
    //    {
    //        foreach (DepartmentInfo department in Department.GetList("RIGHT(DepartmentID, 2) = '00'"))
    //        {
    //            ddlDepartment.Items.Add(new ListItem(department.FullName, department.DepartmentID));
    //        }
    //    }
    //    else
    //    {
    //        ddlDepartment.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionLevel, permissionUnit);
    //        ddlDepartment.DataTextField = "FullName";
    //        ddlDepartment.DataValueField = "DepartmentID";
    //        ddlDepartment.DataBind();

    //    }
    //}
    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel) || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            departmentList = Department.GetList("DepartmentID='" + departmentID.Substring(0, 4) + "000'");
        return departmentList;
    }

    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindWater(sqlString, 10, int.Parse(JumpList.SelectedValue));
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindWater(sqlString, 10, (int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindWater(sqlString, 10, (int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindWater(sqlString, 10, (int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindWater(sqlString, 10, (int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        sqlString = "1=1";
        if (ddlCities.SelectedValue != "")
        {
            sqlString += " and RegionID = '" + ddlCities.SelectedValue + "'";
        }
        if (RoadName.Text != "")
        {
            sqlString += " and RoadName like '%" + RoadName.Text + "%' ";
        }
        string where = "";
        if (UnitList.SelectedValue != "")
        {
            where += where == "" ? "" : " And ";
            where += " DepartmentID='" + UnitList.SelectedValue + "' ";
        }
        else
        {
            if (DepartmentList.SelectedValue == "")
            {
                UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
                DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
                if (userInfo.DepartmentID.Substring(0, 4) != "1300")
                {
                    where += where == "" ? "" : " And ";
                    where += " DepartmentID like '" + userInfo.DepartmentID.Substring(0, 4) + "%' ";
                }
            }
            else
            {
                where += where == "" ? "" : " And ";
                where += " DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' ";
            }
        }
        where = where == "" ? "" : " And " + where;
        sqlString += where;
        if (Status.SelectedValue != "-1")
            sqlString += " and Workable=" + Status.SelectedValue + "";

        BindWater(sqlString, 10, 0);
    }
    protected void gvWater_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200800") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200803"))
        {
            if (Water.Delete(gvWater.DataKeys[e.RowIndex].Value.ToString()))
            {
                BindWater(sqlString, 10, 0);
            }
        }
        else
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
    }
    protected void gvWater_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * gvWater.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}
